TARGET=test
CC=arm-none-eabi-gcc
CP=arm-none-eabi-objcopy
VPATH=lib

FLASH_SIZE=-D STM32F10X_HD
LDFLAGS = -T stm32_flash.ld -Wl,-cref,-u,Reset_Handler -Wl,-Map=$(TARGET).map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group -Llib
CFLAGS=-mthumb -mcpu=cortex-m3 -g -o
HARD = $(patsubst %.c,%.o,$(wildcard hw/*.c))

$(TARGET).hex:$(TARGET).elf
	$(CP) $< -Oihex $@

$(TARGET).elf:startup_stm32f10x_hd.o main.o $(HARD)
	$(CC) $^ $(LDFLAGS) $(CFLAGS) $@

lib/startup_stm32f10x_hd.o:startup_stm32f10x_hd.s
	$(CC) -c $< $(CFLAGS) $@

main.o:main.c 
	$(CC) -Ihw -Ilib -c $< $(CFLAGS) $@ $(FLASH_SIZE)

hw/%.o:hw/%.c 
	$(CC) -Ilib -c $< $(CFLAGS) $@ $(FLASH_SIZE) 

.PHONY: bin hex flash clean
bin:
	$(CP) $(TARGET).elf $(TARGET).bin

hex:
	$(CP) $(TARGET).elf -Oihex $(TARGET).hex

flash:
	st-flash --format ihex write $(TARGET).hex

clean:
	rm -f *.o $(TARGET).*
